package com.zw.yzk.kabu.view.activity;

import android.text.TextUtils;
import android.widget.EditText;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.data.cache.db.DataBaseManager;
import com.zw.yzk.kabu.data.cache.db.User;
import com.zw.yzk.kabu.data.cache.store.UserStore;
import com.zw.yzk.kabu.internal.component.DaggerAuthCoserComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.AuthCoserModule;
import com.zw.yzk.kabu.management.AppManager;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.presenter.AuthCoserPresenter;
import com.zw.yzk.kabu.view.view.AuthCoserView;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2017/7/6.
 */

public class AuthCoserActivity extends BaseActivity implements AuthCoserView {

    @Bind(R.id.name)
    EditText name;
    @Bind(R.id.card_no)
    EditText cardNo;

    @Inject
    AuthCoserPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_auth_coser;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.account_auth))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerAuthCoserComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .authCoserModule(new AuthCoserModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destroy();
    }

    @OnClick(R.id.auth)
    public void auth() {
        String nameStr = name.getText().toString();
        String idStr = cardNo.getText().toString();
        if (TextUtils.isEmpty(nameStr)) {
            ToastManager.getInstance().showToast(this, this.getResources().getString(R.string.please_input_name));
        } else if (TextUtils.isEmpty(idStr)) {
            ToastManager.getInstance().showToast(this, this.getResources().getString(R.string.please_input_id));
        } else {
            LoadingDialogManager.getInstance().showLoadingDialog(this);
            presenter.auth(nameStr, idStr);
        }
    }

    @Override
    public void authSuccess() {
        User user = DataBaseManager.getInstance().getLoginUser();
        if (user == null) {
            AppManager.logout(this);
        } else {
            user.setIsFame(true);
            new UserStore().updateUserInfo(user);
            finish();
        }
    }
}
